/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graphic;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import puntajes.Puntajes;

/**
 *
 * @author maru
 */
public class ScorePanel extends JPanel{
    /**
	 * 
	 */
	private JButton volver;
    private StartPanel panel;
    private StartFrame contFrame;
    private Puntajes puntajes;
    private JComboBox nivelN;
    private JButton ok;
    private String[] sp;
      
    public ScorePanel(StartFrame contFrame, StartPanel panel) {
    	this.panel=panel;
    	this.contFrame=contFrame;
        setLayout(null);
        contFrame.setBounds(1, 1, 600, 600);
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        contFrame.setLocation(size.width/2 - 600/2, size.height/2 - 600/2);

		File arch= new File(java.lang.System.getProperty("user.dir") + "/levels/");
		File aux[]=arch.listFiles();
		Set<File> archPuntajes= new TreeSet<File>(new Comparator<File>(){
			public int compare(File f1, File f2) {
				return f1.getName().compareTo(f2.getName());
			}
		});
		int i=0;
		for(File archivo: aux)
			if(archivo.getName().contains(".scores"))
				archPuntajes.add(archivo); 
		
		sp = new String[archPuntajes.size()];
		for(File auxi: archPuntajes)
			sp[i++]= auxi.getName().substring(0, auxi.getName().indexOf(".scores"));
			
		JLabel nivel = new JLabel("Mostrar puntajes de:");
        nivel.setBounds( 20, 20, 150, 30);
        add(nivel);

        nivelN = new JComboBox(sp);
        nivelN.setBounds(150, 20, 150, 30);
        nivelN.setSelectedIndex(0);
        add(nivelN);

        volver = new JButton("Volver");
        volver.setBounds( 450, 500, 100, 30);
        add(volver);
        
        ok = new JButton("Ver");
        ok.setBounds(400, 20, 100, 30);
        add(ok);

        volver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	volverActionPerformed();
            }
        });
        nivelN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            nivelNActionPerformed();
            }
        });
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            okActionPerformed();
            }
        });
    }
    
    private int nivelNActionPerformed(){
        return nivelN.getSelectedIndex();
    }
    
    private void volverActionPerformed(){
    	ScorePanel.this.contFrame.remove(ScorePanel.this);
        ScorePanel.this.panel.setVisible(true);
        
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        ScorePanel.this.contFrame.setBounds(size.width/2 - 400/2, size.height/2 - 400/2, 400, 400);
    }
    private void okActionPerformed(){
    	int pos=nivelNActionPerformed();
    	try {
			puntajes = new Puntajes( sp[pos]+".scores");
		} catch (IOException e) {
			e.printStackTrace();
		}
        ScoresPanel nivelScore = new ScoresPanel(puntajes.getLevelPunt());
        this.contFrame.add(nivelScore);
        this.add(nivelScore);
        nivelScore.setBounds(1, 101, 600, 400);
        
        nivelScore.setVisible(true);	
	
    }
}
